package com.zdw.oauth.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zdw.common.parent.JSONResult;
import com.zdw.oauth.config.international.InternationalUtils;
import com.zdw.oauth.dto.DepartmentAddDto;
import com.zdw.oauth.dto.DepartmentSelectDto;
import com.zdw.oauth.dto.DepartmentUpdDto;
import com.zdw.oauth.dto.UserDepartmentDto;
import com.zdw.oauth.service.DepartmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author 卓德文
 * @since 2023-07-25
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    /**
     * 新增部门
     *
     * @return
     */
    @ApiOperation(value = "新增部门")
    @PostMapping("/add")
    @SaCheckLogin
    @SaCheckPermission(value = "department.add", orRole = {"superAdmin", "admin", "department"})
    public JSONResult add(@RequestBody DepartmentAddDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), departmentService.add(dto));
    }

    /**
     * 编辑部门
     *
     * @return
     */
    @ApiOperation(value = "编辑部门")
    @PutMapping("/update")
    @SaCheckLogin
    @SaCheckPermission(value = "department.update", orRole = {"superAdmin", "admin", "department"})
    public JSONResult update(@RequestBody DepartmentUpdDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), departmentService.update(dto));
    }

    /**
     * 删除部门
     *
     * @return
     */
    @ApiOperation(value = "删除部门")
    @DeleteMapping("/delete")
    @SaCheckLogin
    @SaCheckPermission(value = "department.delete", orRole = {"superAdmin", "admin", "department"})
    public JSONResult delete(@RequestBody List<Integer> departmentIdList) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), departmentService.delete(departmentIdList));
    }

    /**
     * 查询部门信息
     *
     * @return
     */
    @ApiOperation(value = "查询部门信息")
    @PostMapping("/select")
    @SaCheckPermission(value = "department.select", orRole = {"superAdmin", "admin", "department"})
    public JSONResult select(@RequestParam int id) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), departmentService.select(id));
    }

    /**
     * 分页查询部门列表
     *
     * @return
     */
    @ApiOperation(value = "分页查询部门列表")
    @PostMapping("/page")
    @SaCheckPermission(value = "department.select", orRole = {"superAdmin", "admin", "department"})
    public JSONResult page(@RequestBody DepartmentSelectDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), departmentService.page(dto));
    }

    /**
     * 查询部门树
     *
     * @return
     */
    @ApiOperation(value = "查询部门树")
    @PostMapping("/tree")
    @SaCheckPermission(value = "department.select", orRole = {"superAdmin", "admin", "department"})
    public JSONResult tree() {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), departmentService.tree());
    }

    /**
     * 给用户分配部门
     *
     * @return
     */
    @ApiOperation(value = "给用户分配部门")
    @PostMapping("/grantDepartment")
    @SaCheckPermission(value = "department.grant", orRole = {"superAdmin", "admin", "department"})
    public JSONResult grantDepartment(@RequestBody UserDepartmentDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), departmentService.grantDepartment(dto));
    }

}
